//
//  WmcAlertLayoutGroupView.swift
//  YPWatermarkCamera
//
//  Created by Json on 2023/8/1.
//

import UIKit
import RxRelay

class WmcAlertLayoutGroupView: WmcLayoutAlertBaseGroupView {

    /// collectionView
    private lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal
        let itemH = collcetionH - 1.0
        let spacing = 32.0.wmc_autoHor
        layout.itemSize = CGSize(width: itemW, height: itemH)
        layout.minimumLineSpacing = spacing
        let collectionV = UICollectionView(frame: .zero, collectionViewLayout: layout)
        collectionV.backgroundColor = UIColor.clear
        return collectionV
    }()
    
    private let collcetionH = 73.0.wmc_autoHor
    private let itemW = 56.0.wmc_autoHor
    
    /// 数据源
    private lazy var layouts: [WmcPhotoLayout] = {
        let _layouts = WmcPhotoLayout.Columns.allCases.map { column -> WmcPhotoLayout in
            let layout = WmcPhotoLayout(columns: column)
            layout.photoNum = column.photoNum
            layout.itemW = itemW
            return layout
        }
        return _layouts
    }()
    /// 当前选中的layout
    private(set) var selectedLayout: WmcPhotoLayout?
    
    /// 排版列数
    var columns: WmcPhotoLayout.Columns = .one {
        didSet {
            let layout = layouts.first { $0.columns == columns }
            if let layout = layout {
                didSelected(layout: layout)
            }
        }
    }
    /// 选中排版回调
    let didSelectedLayout: PublishRelay<WmcPhotoLayout> = .init()
    
    // MARK: UI
    override func makeUI() {
        super.makeUI()
        titleLabel.text = "排列"
        // collectionView
        stackView.addArrangedSubview(collectionView)
        collectionView.snp.makeConstraints { make in
            make.height.equalTo(collcetionH)
        }
        //
        collectionView.wmc_register(cellClass: WmcPhtotoLayoutCell.self)
        collectionView.dataSource = self
        collectionView.delegate = self
    }

}

extension WmcAlertLayoutGroupView {
    
    
    /// 选择排版后调用
    private func didSelected(layout: WmcPhotoLayout) {
        selectedLayout?.isSelected = false
        layout.isSelected = true
        selectedLayout = layout
        collectionView.reloadData()
    }
}


// MARK: UICollectionViewDataSource,UICollectionViewDelegateFlowLayout
extension WmcAlertLayoutGroupView: UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return layouts.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let layout = layouts[indexPath.item]
        let cell = collectionView.wmc_dequeueReusableCell(cellClass: WmcPhtotoLayoutCell.self, for: indexPath)
        cell.layout = layout
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let layout = layouts[indexPath.item]
        didSelected(layout: layout)
        // 选中回调
        didSelectedLayout.accept(layout)
    }
}

